package com.h3bpm.web.msg.quartz;

import OThinker.H3.Controller.ControllerBase;
import com.h3bpm.web.msg.dao.TblSmsDao;
import com.h3bpm.web.msg.mobset.bean.msmResultBean;
import com.h3bpm.web.msg.service.MsgService;
import com.h3bpm.web.msg.service.TblSmsService;
import com.h3bpm.web.msg.tempuri.SmsIDGroup;
import com.h3bpm.web.msg.tempuri.holders.ArrayOfSmsIDListHolder;
import com.h3bpm.web.msg.util.StringUtils;
import com.h3bpm.web.organization.service.OrganizationSyncService;
import data.DataRow;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 发送短信定时任务
 */
@Component
public class SendMsgQuartz extends ControllerBase {

    private static final Logger log = LoggerFactory.getLogger(SendMsgQuartz.class);

    @Autowired
    private TblSmsService tblSmsService;

    @Value("${task.bpm}")
    public String taskBpm;

    @Autowired
    private MsgService msgService;

    /**
     * 发送短信
     */
//	 生产服务器BPM1定时任务:
//	 @Scheduled(cron = "27 */4 * * * ?")
//	 生产服务器BPM2定时任务:
    @Scheduled(cron = "24 */5 * * * ?")
    public void sendMsg() {
        if("0".equals(taskBpm)){
            return;
        }
        log.info("发送短信开始执行,{}");
        try {
            List<DataRow> list = tblSmsService.getNoSendSmsList();
            for (DataRow dataRow : list) {
                msgService.SendMsg2(dataRow.getString("Number"),dataRow.getString("Message"));
                tblSmsService.updateSms(dataRow.getString("ObjectID"));
                /*if(dataRow.getString("Number")!=null&&!dataRow.getString("Number").equals("")){
                    //发送消息
                    msmResultBean msmResultBean = msgService.SendMsg2(dataRow.getString("Number"),dataRow.getString("Message"));
                    SmsIDGroup[] smsIDGroup = msmResultBean.getSmsIDList().value;
                    //当短信SmsID大于0时，表示此条记录发送成功
                    if(smsIDGroup!=null&&smsIDGroup.length>0&&smsIDGroup[0].getSmsID()>0){
                        //更新该条数据状态为已发送
                        tblSmsService.updateSms(dataRow.getString("ObjectID"));
                    }else{
                        log.error("发送短信失败,详细信息："+msmResultBean.getErrMsg().value);
                    }
                }*/
            }
        } catch (Exception e) {
            log.error("发送短信失败,详细信息："+e);
            e.printStackTrace();
        }
        log.info("发送短信成功,{}");
    }
    /**
     * 发送短信
     */
//	 生产服务器BPM1定时任务:
	 @Scheduled(cron = "27 */4 * * * ?")
//	 生产服务器BPM2定时任务:
//    @Scheduled(cron = "24 */5 * * * ?")
    public void sendMsg2() {
         if("1".equals(taskBpm)){
             return;
         }
        log.info("发送短信开始执行,{}");
        try {
            List<DataRow> list = tblSmsService.getNoSendSmsList();
            for (DataRow dataRow : list) {
                msgService.SendMsg2(dataRow.getString("Number"),dataRow.getString("Message"));
                tblSmsService.updateSms(dataRow.getString("ObjectID"));
                /*if(dataRow.getString("Number")!=null&&!dataRow.getString("Number").equals("")){
                    //发送消息
                    msmResultBean msmResultBean = msgService.SendMsg2(dataRow.getString("Number"),dataRow.getString("Message"));
                    SmsIDGroup[] smsIDGroup = msmResultBean.getSmsIDList().value;
                    //当短信SmsID大于0时，表示此条记录发送成功
                    if(smsIDGroup!=null&&smsIDGroup.length>0&&smsIDGroup[0].getSmsID()>0){
                        //更新该条数据状态为已发送
                        tblSmsService.updateSms(dataRow.getString("ObjectID"));
                    }else{
                        log.error("发送短信失败,详细信息："+msmResultBean.getErrMsg().value);
                    }
                }*/
            }
        } catch (Exception e) {
            log.error("发送短信失败,详细信息："+e);
            e.printStackTrace();
        }
        log.info("发送短信成功,{}");
    }

    @Override
    public String getFunctionCode() {
        return null;
    }
}
